﻿using System;
using System.Web.UI.WebControls;
using Bingosoft.Modules.SecurityConsole.Organization;
using Bingosoft.Web.UI.WebControls;
using Bingosoft.Web.Utils;

namespace Bingosoft.WebApp.Modules.SecurityConsole.Organization
{
    public partial class OrganizationProcessRoleList : System.Web.UI.BasePage
    {
        private OrganizationService _service;
        private OrganizationService Service
        {
            get
            {
                return _service ?? (_service = ObjectHelper.GetObject<OrganizationService>());
            }
        }

        protected void SearchGrid(object sender, EventArgs e)
        {
            Refresh();
        }

        protected void RemoveRolesFromUser(object sender, EventArgs e)
        {
            string orgId = Request.QueryString["OrgId"];
            string[] roleIds = SmartGridView1.GetCheckedValues();
            Service.RemoveProcessRolesForOrganization(orgId, roleIds);
            Refresh();
        }

        protected void RemoveRoleFromUser(object sender, EventArgs e)
        {
            ImageButton img = (ImageButton)sender;
            string roleId = img.Attributes["_KeyId"];
            string orgId = Request.QueryString["OrgId"];
            Service.RemoveProcessRoleForOrganization(orgId, roleId);
            Refresh();
        }

        private void Refresh()
        {
            this.SmartGridView1.DataBind();
        }

        protected void SmartGridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton ibtn = (ImageButton)e.Row.FindControl("ibtnAssign");
                string type = e.Row.Cells[2].Text;
                string wfRoleId = ibtn.Attributes["_KeyId"];
                string name = ibtn.Attributes["_KeyName"];
                string script;
                if (type == "用户")
                {
                    ibtn.ToolTip = @"添加用户";
                    script =
                        "document.frames['iframeUserOrOrg'].location.href='AddOrganizationProcessUser.aspx?Id=" +
                        wfRoleId + "&Name=" + name + "';return false;";
                }
                else
                {
                    script = "document.frames['iframeUserOrOrg'].location.href='AddOrganizationProcessOrg.aspx?Id=" +
                             wfRoleId + "&Name=" + name + "';return false;";
                }
                ibtn.Attributes.Add("onclick", script);
                e.Row.Cells[1].Attributes.Add("onclick", script);
                e.Row.Cells[2].Attributes.Add("onclick", script);
                e.Row.Cells[3].Attributes.Add("onclick", script);
            }

        }


        protected void SmartGridView1_GridView_RenderEnd(object sender, System.Web.UI.HtmlTextWriter writer)
        {
            SmartGridView grid = (SmartGridView)sender;
            if (grid.Rows.Count == 0)
            {
                iframeUserOrOrg.Visible = false;
            }
            else
            {
                HiddenField hiddenField = (HiddenField)grid.Rows[0].Cells[3].FindControl("HFWFroleId");
                string wfRoleId = hiddenField.Value;
                string type = grid.Rows[0].Cells[2].Text;
                string name = grid.Rows[0].Cells[1].Text;
                iframeUserOrOrg.Visible = true;
                if (type == "用户")
                {
                    iframeUserOrOrg.Attributes["src"] = "AddOrganizationProcessUser.aspx?Id=" + wfRoleId + "&Name=" + name;
                }
                else
                {
                    iframeUserOrOrg.Attributes["src"] = "AddOrganizationProcessOrg.aspx?Id=" + wfRoleId + "&Name=" + name;
                }
            }
        }
    }
}